define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/tableyue/index' + location.search,
                    add_url: 'shop/tableyue/add',
                    edit_url: 'shop/tableyue/edit',
                    del_url: 'shop/tableyue/del',
                    multi_url: 'shop/tableyue/multi',
                    import_url: 'shop/tableyue/import',
                    table: 'table_yue',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                singleSelect: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {
                            field: 'shop_id',
                            title: __('店铺名称'),
                            operate: 'IN',
                            align: 'left',
                            searchList: $.getJSON('shop/tableyue/getTqShop'),
                            visible: false
                        },
                        {field: 'shop_name', title: __('店铺名称'), operate: false, align: 'left'},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE'},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE'},
                        // {field: 'tag', title: __('Tag'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        // {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {
                            field: 'create_time',
                            title: __('发起时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'user.mobile', title: __('约球发起人')},
                        {field: 'reciver.mobile', title: __('应约人')},
                        {
                            field: 'tableyue.status',
                            title: __('Status'),
                            searchList: {
                                "payed": __('已支付'),
                                "wait": __('等待应约'),
                                "doing": __('进行中'),
                                "done": __('已完成'),
                                "cancel": __('已取消')
                            },
                            formatter: function (value, row, index) {
                                // console.log(row.status);
                                var statusVal = row.status;
                                if (statusVal === 'payed') {
                                    return '<span style="color:red">已支付</span>';
                                } else if (statusVal === 'cancel') {
                                    return '<span style="color:grey">已取消</span>';
                                } else if (statusVal === 'wait') {
                                    return '<span style="color:green">等待邀约</span>';
                                } else if (statusVal === 'doing') {
                                    return '<span style="color:blue">进行中</span>';
                                } else if (statusVal === 'done') {
                                    return '<span style="color:darkolivegreen">进行中</span>';
                                } else {
                                    return '其他';
                                }
                            }
                        },
                        {
                            field: 'type',
                            title: __('付款类型'),
                            searchList: {
                                "AA付款": __('AA付款'),
                                "赢家付款": __('赢家付款'),
                                "输家付款": __('输家付款')
                            },
                        },
                        {
                            field: 'table_type',
                            title: __('台桌类型'),
                            searchList: {"中式台球": __('中式台球'), "斯洛克": __('斯洛克')}
                        },
                        {
                            field: 'start_time',
                            title: __('开始时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'mark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'level',
                            title: __('邀约类型'),
                            operate: 'LIKE',
                            searchList: {"萌新": __('萌新'), "业余": __('业余'), "专业": __('专业')}
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
